<template>
	<div class="personnelList">
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/' }"><i class="lh lh-sys fw_100"></i></el-breadcrumb-item>
			<el-breadcrumb-item>系统设置</el-breadcrumb-item>
			<el-breadcrumb-item>{{ title }}</el-breadcrumb-item>
		</el-breadcrumb>
		<div class="content_Box box-shadow mt_15">
			<h4 class="title">{{ title }}</h4>
			<Subsidy-add @sendValue="sendValue" :identity="identity" :id="id"></Subsidy-add>
		</div>
	</div>
</template>
<script>
import SubsidyAdd from './components/SubsidyAdd.vue'
import { subsidyShopCreate, subsidyAreaCreate } from '@/api/subsidy.js'

export default {
	name: 'subsidy-add',
	data() {
		return {
			id: '',
			identity: '',
			title: '',
			query: ''
		}
	},
	components: {
		SubsidyAdd
	},
	async created() {
		const { identity, id } = this.$route.query
		this.id = id
		this.identity = identity
		if (identity == 'shop') {
			this.title = !id ? '新增单店补贴' : '查看单店补贴'
		} else if (identity == 'area') {
			this.title = !id ? '新增商圈补贴' : '查看商圈补贴'
		}
	},

	mounted() {},

	methods: {
		sendValue(val) {
			this.query = val
			this.create()
		},
		async create() {
			try {
				if (this.identity == 'shop') {
					await subsidyShopCreate(this.computedParams())
				} else if (this.identity == 'area') {
					await subsidyAreaCreate(this.computedParams())
				}

				this.$message.success('添加成功')
				this.goBack()
			} catch (err) {
				console.log('创建补贴error:', err)
			}
		},

		computedParams(params = {}) {
			let { level10, level20, level30, ...rest } = this.query
			return {
				...rest,
				level10: +(level10 * 100).toFixed(2),
				level20: +(level20 * 100).toFixed(2),
				level30: +(level30 * 100).toFixed(2),
				...params
			}
		},
		// 返回
		goBack() {
			this.$router.go(-1)
		}
	}
}
</script>

<style scoped lang="scss">
.title {
	border-bottom: 1px solid #ebebeb;
	padding: 0 0 10px 10px;
	margin-bottom: 20px;
}
</style>



// WEBPACK FOOTER //
// src/components/subsidy/Add.vue